/*
*/

#include <conio.h>
#include <stdio.h>
#include <math.h>

#define MAX 100

int rear, front;
int queue[MAX];

int n = 0, v = 1;
int a[MAX][MAX];
int chuaxet[MAX];

void push(int value){
	queue[front++] = value;
}

int pop(){
	return queue[rear++];
}

int isEmpty(){
	return (rear >= front);
}
int isFull(){
	if (front >= MAX) return 1;
	return 0;
}

void init(){
	for(int i  = 0; i<n; i++){
		chuaxet[i] = 1;
	}
	front = rear = 0;
}
void input(){
	FILE * fp;
	fp = fopen("bfs.in", "r");

	fscanf(fp,"%d %d", &n, &v);

	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			fscanf(fp, "%d", &a[i][j]);
		}
	}
	init();
	fclose(fp);
}



void output(){
	FILE * fp;
	fp = fopen("bfs.ou", "w");
	fprintf(fp, "STEP			BFS\n");
	fclose(fp);
}

int step = 0;

void print_ln(){
	FILE * fp;
	fp = fopen("bfs.ou", "a");
	fprintf(fp, "%d\t\t\t", ++step);
	for(int i = 0; i < front; i++){
		fprintf(fp, "%d,", queue[i]+1);
	}
	fprintf(fp, "\n");
	fclose(fp);

}

void BFS(int u){
	push(u);
	chuaxet[u] = 0;
	while (!isEmpty()){
		v = pop();
		print_ln();
		for(int i = 0; i<n; i++){
			if(a[v][i] > 0 && chuaxet[i]){
				push(i);
				chuaxet[i] = 0;
			}
		}
	}
}

void main(void){
	input();
	output();
	BFS(v - 1);
}